package gameUi
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;

	public class Isume
	{
		private var _chipMC:MovieClip;//保险筹码容器类
		private var _chipNum:Number = 0//筹码数
		
		private var _tf0:TextFormat;//筹码字体设置
		
		/**
		 * 保险筹码容器类
		 **/
		public function Isume(chipMC:MovieClip)
		{
			_chipMC = chipMC;
			
			_tf0 = new TextFormat();
			_tf0.size = 9;
		}
		
		/**
		 * 加注筹码
		 **/
		public function addChip(num:Number):void{
			BlackJack._gameUi.allChipNum += num;
//			if(BlackJack._gameUi.allChipNum == 0) BlackJack._gameUi.gameStateChange(0);
			
			trace("_isumechipNum:",_chipNum,num);
			_chipNum += num;
			changChipNum();
		}
		
		/**
		 * 筹码值变更
		 **/
		public function changChipNum():void{
			while(_chipMC.numChildren>0){
				_chipMC.removeChildAt(0);
			}
			
			var tempChipNum:Number = _chipNum;
			var i:int
			var chip:ChipObject;
			if(_chipNum == 0) return;
			var numText:Sprite = pointTextShow(_chipNum);
			
			var tempNum:Number = Math.floor(tempChipNum/1000);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(9);
				chip.chipNumber.gotoAndStop(20);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*1000;
			
			tempNum = Math.floor(tempChipNum/500);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(8);
				chip.chipNumber.gotoAndStop(19);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*500;
			
			tempNum = Math.floor(tempChipNum/100);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(7);
				chip.chipNumber.gotoAndStop(16);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*100;
			
			tempNum = Math.floor(tempChipNum/25);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(6);
				chip.chipNumber.gotoAndStop(14);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*25;
			
			tempNum = Math.floor(tempChipNum/10);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(5);
				chip.chipNumber.gotoAndStop(12);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*10;
			
			tempNum = Math.floor(tempChipNum/5);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(4);
				chip.chipNumber.gotoAndStop(11);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*5;
			
			tempNum = Math.floor(tempChipNum/1);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(3);
				chip.chipNumber.gotoAndStop(8);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*1;
			
			if(tempChipNum == 0.5){
				chip = new ChipObject();
				chip.gotoAndStop(1);
				chip.chipNumber.gotoAndStop(7);
				_chipMC.addChild(chip);
			}
			
			for(i = 0;i<_chipMC.numChildren;i++){
				_chipMC.getChildAt(i).y = -i*3;
			}
			
			_chipMC.addChild(numText);
			
			SoundManager.instance.play("snd_ChipStack2");
		}
		
		//筹码点数显示
		private function pointTextShow(num:Number):Sprite{
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x323232,0.8);
			spr.graphics.drawRect(0,0,20,10);
			var txt:TextField = new TextField();
			txt.textColor = 0xffffff;
			txt.defaultTextFormat = _tf0;
			trace(num,num.toString());
			txt.text = num.toString();
			txt.width = 20;
			txt.y = -3;
			txt.autoSize = TextFieldAutoSize.CENTER;
			spr.addChild(txt);
			spr.x = -10;
			spr.y = 20;
			return spr;
		}
		
		/**
		 * 重置保险
		 **/
		public function initIsume():void{
			_chipNum = 0;
			while(_chipMC.numChildren>0){
				_chipMC.removeChildAt(0);
			}
		}
		
		//保险结算
		public function isumAction(win:Boolean):void{
			var point:Point = new Point();
			if(win){
//				 = _chipMC.globalToLocal(new Point(BlackJack._gameUi.pointPanel.x,BlackJack._gameUi.pointPanel.y));
				point.x =  BlackJack._gameUi.pointChipWin.x - _chipMC.x;
				point.y =  BlackJack._gameUi.pointChipWin.y - _chipMC.y;
				for(var i:int = 0;i<_chipMC.numChildren;i++){
					TweenLite.to(_chipMC.getChildAt(i),0.5,{x:point.x,y:point.y});
				}
				BlackJack._gameUi.bankerChipPay(_chipNum*2);
				setTimeout(closeChipAction,400);
			}else{
//				 point = _chipMC.globalToLocal(new Point(BlackJack._gameUi.pointBankerChip.x,BlackJack._gameUi.pointBankerChip.y));
				 point.x = BlackJack._gameUi.pointBankerChip.x - _chipMC.x;
				 point.y = BlackJack._gameUi.pointBankerChip.y - _chipMC.y;
				for(i = 0;i<_chipMC.numChildren;i++){
					TweenLite.to(_chipMC.getChildAt(i),0.5,{x:point.x,y:point.y});
				}
			}
			setTimeout(closeCardActionOver,500);
		}
		
		private function closeCardActionOver():void{
			while(_chipMC.numChildren>0){
				_chipMC.removeChildAt(0);
			}
			setTimeout(setDealing,2000);
		}
		
		private function setDealing():void{
			BlackJack._gameUi.closeDealing(false);
		}
		
		private function closeChipAction():void{
			BlackJack._gameUi.playerChipCome(_chipNum);
		}
	}
}